<template>
	<view class="login">
		<image src='http://img-test.gz-yami.com/mini/logo.jpg' class='c-logo'></image>
		<view class="title">mall4j</view>
		<view class="login-form">
			<view class="item">
				<view class="account">
					<text class="input-item">用户名</text>
					<input type="text" data-type="account" @input="userInput" :value="username" placeholder="请输入用户名" />
				</view>
			</view>
			<view class="item">
				<view class="account">
					<text class="input-item">密码</text>
					<input type="password" data-type="password" @input="passInput" :value="password"
						placeholder="请输入密码" />
				</view>
			</view>
			<view>
				<button class="authorized-btn" bindtap="handleLoginOrRegister"
					@click="submitLogin()">{{isRegister ? '登陆': '注册'}}</button>
				<button class="to-idx-btn" @click="back()">回到首页</button>
			</view>
			<view class="operate">
				<view class="to-register" bindtap="handleChangeShowType" @click="switch_login()">
					{{isRegister?'没有账号？去注册>' :'已有账号？去登录>' }}
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		encrypt
	} from "./encrypt.js"
	export default {
		data() {
			return {
				username: "",
				password: '',
				isRegister: true
			}
		},
		methods: {
			userInput(e) {
				console.log(e, "11111");
				this.username = e.target.value
			},
			passInput(e) {
				console.log(e, "22222");
				this.password = e.target.value
			},
			submitLogin() {
				if (this.username == '') {
					uni.showToast({
						title: "请输入账号",
						icon: "none"
					})
					return
				}
				if (this.password == "") {
					uni.showToast({
						title: "请输入密码",
						icon: "none"
					})
					return
				}
				const BASE_URL = 'https://yami-mini-api.yiluxdeng.com'
				const params = {
					url: this.isRegister ? BASE_URL + '/login' : BASE_URL + '/user/register',
					method: "POST",
					data: {
						userName: this.username,
						passWord: encrypt(this.password)
					}
				}
				uni.request(params).then(res => {
					console.log(res, "res")
					if (res.statusCode == 200) {
						if (this.isRegister) {
							uni.switchTab({
								url: "../../pages/tabBar/Home/Home"
							})
							uni.showToast({
								title: "登陆成功",
							})
							uni.setStorageSync("token", res.data.accessToken)
						} else {
							uni.showToast({
								title: '注册成功'
							})
							this.username = ''
							this.password = ''
							this.isAccount = true
						}

					} else {
						uni.showToast({
							icon: 'error',
							title: '账号或密码错误'
						})
					}
				})
			},
			switch_login() {
				this.isRegister = !this.isRegister
			},
			back() {
				uni.switchTab({
					url: "../../pages/tabBar/Home/Home"
				})
			}
		},

	}
</script>

<style scoped>
	@import url("login.css");
	/* .login {
		width: 100%;
		height: 100%;
		box-sizing: border-box;
		display: flex;
		flex-direction: column;
		align-items: center;
		padding-top: 100rpx;
	}

	.my_img {
		width: 180rpx;
		height: 180rpx;
		border-radius: 50%;
		overflow: hidden;
		padding: -200rpx;
	}

	.my_title {
		font-size: 36rpx;
		line-height: 84rpx;
		margin-top: 20px;
	}

	.p {
		color: #a5a5a5;
	}

	button {
		width: 80%;
		margin-top: 20rpx;
		background: #eb2444;
		color: #fff;
		line-height: 60rpx;
		font-size: 30rpx;
	} */
</style>
